package com.androidwwf.jwr;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class PlayerDataActivity extends Activity {

	private TextView tvPoints, tvAmountQuiz, tvAmountPicture, tvLevel;
	private PlayerStatistics ps;
	private Context thiscontext;
	
	private String[] levelnamezero = new String[] { "Achievement Locked", "Achievement Locked", "Achievement Locked", "Achievement Locked", "Achievement Locked", "Achievement Locked" };
	private String[] levelnameone = new String[] { "Unlocked at Rookie level", "Achievement Locked", "Achievement Locked", "Achievement Locked", "Achievement Locked", "Achievement Locked" };
	private String[] levelnametwo = new String[] { "Unlocked at Rookie level", "Unlocked at Ranger level", "Achievement Locked", "Achievement Locked", "Achievement Locked", "Achievement Locked" };
	private String[] levelnamethree = new String[] { "Unlocked at Rookie level", "Unlocked at Ranger level", "Unlocked at Forest Gump level", "Achievement Locked", "Achievement Locked", "Achievement Locked" };
	private String[] levelnamefour = new String[] { "Unlocked at Rookie level", "Unlocked at Ranger level", "Unlocked at Forest Gump level", "Unlocked at Tree Huger level", "Achievement Locked", "Achievement Locked" };
	private String[] levelnamefive = new String[] { "Unlocked at Rookie level", "Unlocked at Ranger level", "Unlocked at Forest Gump level", "Unlocked at Tree Huger level", "Unlocked at Expert level", "Achievement Locked" };
	private String[] levelnamesix = new String[] { "Unlocked at Rookie level", "Unlocked at Ranger level", "Unlocked at Forest Gump level", "Unlocked at Tree Huger level", "Unlocked at Expert level", "Unlocked at Nature Freak level" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.playerdata);
		init();

		GridView gridview = (GridView) findViewById(R.id.gridview1);
		gridview.setAdapter(new ImageAdapter(this));
		gridview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				//Code voor klikken.
				
				makeToast(position, getLevelArray());
			}
		});

	}

	private void init() {
		ps = new PlayerStatistics(this);
		thiscontext = this;
		tvPoints = (TextView) findViewById(R.id.tvPoints);
		tvAmountQuiz = (TextView) findViewById(R.id.tvAmountQuiz);
		tvAmountPicture = (TextView) findViewById(R.id.tvAmountPicture);
		tvLevel = (TextView) findViewById(R.id.tvLevel);

		tvPoints.setText("Total points earned: "
				+ String.valueOf(ps.getPlayerPoints()));
		tvAmountQuiz.setText("Total amount of quiz game played: "
				+ String.valueOf(ps.getPlayerQuiz()));
		tvAmountPicture
				.setText("Total amount of compare the picture game played: "
						+ String.valueOf(ps.getPlayerPictures()));
		tvLevel.setText("Your current player level: " + ps.getPlayerLevel());
	}
	
	
	private void makeToast(int clickedpicture, String[] levels)
	{		
		switch(clickedpicture)
		{
		case 0:
			Toast.makeText(thiscontext, levels[0], 20).show();
			break;
		case 1:
			Toast.makeText(thiscontext, levels[1], 20).show();
			break;
		case 2:
			Toast.makeText(thiscontext, levels[2], 20).show();
			break;
		case 3:
			Toast.makeText(thiscontext, levels[3], 20).show();
			break;
		case 4:
			Toast.makeText(thiscontext, levels[4], 20).show();
			break;
		case 5:
			Toast.makeText(thiscontext, levels[5], 20).show();
			break;
		}
	}

	public class ImageAdapter extends BaseAdapter {

		private Context mContext;

		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return getImageArray().length;
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			ImageView imageView;
			if (convertView == null) {
				imageView = new ImageView(mContext);
				imageView.setLayoutParams(new GridView.LayoutParams(180, 180));
				imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
				imageView.setPadding(6, 6, 6, 6);
			} else {
				imageView = (ImageView) convertView;
			}
			imageView.setImageResource(getImageArray()[position]);

			return imageView;
		}
		
		private Integer[] getImageArray()
		{
			// De if statement moet gelijk zijn aan de integer[] levelnames in PlayerStatistics.
			
			if(ps.getPlayerLevel().equals("Rookie"))
			{
				return levelone;
			}
			else if(ps.getPlayerLevel().equals("Ranger"))
			{
				return leveltwo;
			}
			else if(ps.getPlayerLevel().equals("Forest Gump"))
			{
				return levelthree;
			}
			else if(ps.getPlayerLevel().equals("Tree Huger"))
			{
				return levelfour;
			}
			else if(ps.getPlayerLevel().equals("Expert"))
			{
				return levelfive;
			}
			else if(ps.getPlayerLevel().equals("Nature Freak"))
			{
				return levelsix;
			}else
			{
				return levelzero;
			}
		}
		
		private Integer[] levelzero = {R.drawable.questionmark, R.drawable.questionmark,R.drawable.questionmark,R.drawable.questionmark,R.drawable.questionmark,R.drawable.questionmark};
		private Integer[] levelone = {R.drawable.achrookie, R.drawable.questionmark,R.drawable.questionmark,R.drawable.questionmark,R.drawable.questionmark,R.drawable.questionmark};
		private Integer[] leveltwo = {R.drawable.achrookie, R.drawable.achranger ,R.drawable.questionmark,R.drawable.questionmark,R.drawable.questionmark,R.drawable.questionmark};
		private Integer[] levelthree = {R.drawable.achrookie, R.drawable.achranger,R.drawable.achforestgump,R.drawable.questionmark,R.drawable.questionmark,R.drawable.questionmark};
		private Integer[] levelfour = {R.drawable.achrookie, R.drawable.achranger,R.drawable.achforestgump,R.drawable.achtreehuger,R.drawable.questionmark,R.drawable.questionmark};
		private Integer[] levelfive = {R.drawable.achrookie, R.drawable.achranger,R.drawable.achforestgump,R.drawable.achtreehuger,R.drawable.achexpert,R.drawable.questionmark};
		private Integer[] levelsix = {R.drawable.achrookie, R.drawable.achranger,R.drawable.achforestgump,R.drawable.achtreehuger,R.drawable.achexpert,R.drawable.achnaturefreak};
	}
	
	private String[] getLevelArray()
	{
		// De if statement moet gelijk zijn aan de integer[] levelnames in PlayerStatistics.
		
		if(ps.getPlayerLevel().equals("Rookie"))
		{
			return levelnameone;
		}
		else if(ps.getPlayerLevel().equals("Ranger"))
		{
			return levelnametwo;
		}
		else if(ps.getPlayerLevel().equals("Forest Gump"))
		{
			return levelnamethree;
		}
		else if(ps.getPlayerLevel().equals("Tree Huger"))
		{
			return levelnamefour;
		}
		else if(ps.getPlayerLevel().equals("Expert"))
		{
			return levelnamefive;
		}
		else if(ps.getPlayerLevel().equals("Nature Freak"))
		{
			return levelnamesix;
		}else
		{
			return levelnamezero;
		}
	}
}
